#include"clientHeadFile/allClientHead.h"
static void processAndSend(char * buf)
{
    printf("%s\n" , buf);
}
//测试用
static void showMes(userMes * usermes)
{
    printf("allsize : %ld , user size: %ld\n" , sizeof(*(usermes)) - MAX_BUF_FILE , sizeof(usermes->user));
    printf("user account:%s\n",usermes->user.account);
    printf("user password:%s\n",usermes->user.password);
    printf("user cType:%d , user mesType:%d\n" , usermes->cType , usermes->mesType);
    printf("user currPath is %s\n" , usermes->user.currPath);
    printf("contetnSize:%d\n",usermes->contentSize);
    printf("Content:%s " ,usermes->content);
}
void chatControl(epoll_event *list , int sofd , int epo_fd , User * me)
{
    printf("聊天正在开发....\n");
    return ;
}

static void sendToServer(userMes * needToSend)
{

}
static void fileControl(epoll_event * list , int socketFd , int epo_fd , User * me)
{
    printf("client.c /29:fileControl running\n");
    userMes usermes;
    usermes.user = *(me);
    addEvent(STDIN_FILENO , epo_fd);
    //这是控制命令
    char fileControlTypeBuf[20] = {0};
    while(1)
    {
        //sleep(1);
        int num = epoll_wait(epo_fd , list , 16 , -1);
        printf("num : %d\n" , num);
        for(int i = 0 ; i < num ; i++)
        {
            if(list[i].data.fd == STDIN_FILENO)
            {   
                memset(fileControlTypeBuf , 0 , 20);
                read(STDIN_FILENO , fileControlTypeBuf , 20);
                bool flag = processMes(&usermes , fileControlTypeBuf );

                showMes(&usermes);
                if(!flag)
                    printf("client.c /47:指令错误\n");
                else
                {
                    //消息封装没有出错，则上传服务器
                    send(socketFd , &usermes, sizeof(usermes) - MAX_BUF_FILE + usermes.contentSize , 0);
                    printf("总大小是 %ld\n" , sizeof(usermes) - MAX_BUF_FILE + usermes.contentSize);
                }

            }
            if(list[i].data.fd == socketFd)//进行消息分析
            {
                //处理返回消息
                bool flag = processRecvMes(socketFd , me);
            }
            if(0)
            {
                //退出逻辑,必须满足两个条件,无文件消息,输入退出
            }
        }
    }
    epoll_ctl(epo_fd , EPOLL_CTL_DEL , STDIN_FILENO , NULL);
    return ;
}
static void mainProcess(int sofd)
{

    User me;
    if(!login(&me , sofd)){printf("登录失败,即将返回....\n");sleep(3);return;}
    else {printf("登录成功...\n");}

    int epo_fd = epoll_create(1);
    addEvent(sofd , epo_fd);
    epoll_event list[16] = {0};

    while(1)
    {
        easyGui2();
        char flag = my_get();
        switch(flag)
        {
            case '1'://文件模块的功能编写
                //按1先添加事件
                fileControl(list , sofd , epo_fd ,  &me);
                break;
            case '2'://聊天功能编写
                chatControl(list , sofd , epo_fd , &me);
                break;
            case '3':
                return ;
            default:
                printf("输入不规范\n");
                break;
        }
    }
}
int main(int argc, char *argv[])
{                                  
    int sofd = clientConnect("127.0.0.1","8080");
    CHECK_ERROR(sofd , -1 , "服务器连接失败\n");
    while(1)
    {
        easyGui();
        char flag = my_get();
        switch(flag)
        {
        case '1'://登录
            mainProcess(sofd);
            break;
        case '2':
            if(signup(sofd))printf("client.c/ 118:注册成功\n");
            else printf("client.c 119:注册失败\n");
            break;
        case '3':
            printf("结束\n");
            exit(1);
            break;
        default:
            printf("输入不规范\n");
            break;
        }
    }
    return 0;
}

//void initMe(User * me)
//{
//    memset(me , 0 , sizeof(*(me)));
//    strcat(me->name,"SB");
//    return ;
//}
